package com.dsbj.common.msg;


import com.alibaba.fastjson.JSON;
import com.dsbj.common.model.Page;
import com.google.common.collect.Maps;

import java.lang.reflect.Field;
import java.util.*;

/**
 * Created with idea
 * Date: 2017/10/23 0023
 * Time: 16:14
 *
 * @author lvdong
 */

public class DataPageMessage<T> extends BaseResponse {

    protected List<T> aaData;

    private Long timestamp;

    private boolean success;

    private long iTotalRecords;

    private long iTotalDisplayRecords;

    private long sEcho;


    public void setSuccess(boolean success) {
        this.success = success;
    }

    public boolean isSuccess() {
        return success;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public static <T> DataPageMessage<T> error(String message) {
        return error(500, message);
    }

    public static <T> DataPageMessage<T> error(int status, String message) {
        DataPageMessage<T> msg = new DataPageMessage<>();
        msg.message = message;
        msg.status(status);
        msg.success = false;
        return msg.putTimeStamp();
    }

    public DataPageMessage<T> status(int status) {
        this.status = status;
        return this;
    }


    public DataPageMessage<T> data(List<T> result,long total,long totalDisplay,long sEcho) {
        this.aaData = result;
        this.iTotalRecords = total;
        this.iTotalDisplayRecords = totalDisplay;
        this.sEcho = sEcho;
        return this;
    }

    private DataPageMessage<T> putTimeStamp() {
        this.timestamp = System.currentTimeMillis();
        return this;
    }

    public DataPageMessage<T> success(boolean success) {
        this.success = success;
        return this;
    }

    public static <T> DataPageMessage<T> ok(List<T> result) {
        return  ok(result,null);
    }

    public static <T> DataPageMessage<T> ok() {
        return ok(null);
    }

    public static <T> DataPageMessage<T> ok(List<T> result, String msg) {
        return ok(result,result.size(),result.size(),1,msg);
    }

    public static <T> DataPageMessage<T> ok(List<T> result, int total, int totalDisplay, int sEcho, String msg) {
        return new DataPageMessage<T>()
                .data(result,total,totalDisplay,sEcho)
                .message(msg)
                .success(true)
                .putTimeStamp()
                .status(200);
    }


    public static <T> DataPageMessage<T> okWithMsg(String msg){
        return ok(null,msg);
    }



    private DataPageMessage<T> message(String msg) {
        this.message = msg;
        return this;
    }


    private Set<String> getStringListFromMap(Map<Class<?>, Set<String>> map, Class type) {
        return map.computeIfAbsent(type, k -> new HashSet<>());
    }



    public List<T> getAaData() {
        return aaData;
    }

    public void setAaData(List<T> aaData) {
        this.aaData = aaData;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public long getiTotalRecords() {
        return iTotalRecords;
    }

    public void setiTotalRecords(long iTotalRecords) {
        this.iTotalRecords = iTotalRecords;
    }

    public long getiTotalDisplayRecords() {
        return iTotalDisplayRecords;
    }

    public void setiTotalDisplayRecords(long iTotalDisplayRecords) {
        this.iTotalDisplayRecords = iTotalDisplayRecords;
    }

    public long getsEcho() {
        return sEcho;
    }

    public void setsEcho(long sEcho) {
        this.sEcho = sEcho;
    }



    @Override
    public String toString() {
        return JSON.toJSONStringWithDateFormat(this, "yyyy-MM-dd HH:mm:ss");
    }
}
